package org.adream.account.rest.api;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.adream.account.model.LoginModel;
import org.adream.account.model.ResultModel;
import org.adream.account.service.LoginService;
import org.adream.account.util.UserUtil;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/login/")
@Controller
public class LoginApiController {

	@Autowired
	private LoginService loginService;
	
	private final static Logger logger = LoggerFactory.getLogger(LoginApiController.class);
	
	/**
	 * 根据登录方式删除login
	 * @param login
	 * @return
	 */
	@RequestMapping(value = "deleteLoginByLogin", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<String> deleteLoginByLogin(String login) {
		if(StringUtils.isEmpty(login)) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "删除失败,参数为空", null);
		}
		return loginService.deleteLoginByLogin(login);
	}

	/**
	 * 根据user id删除login
	 * @param uid
	 * @return
	 */
	@RequestMapping(value = "deleteLoginByUid", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<String> deleteLoginByUid(String uid) {
		if (StringUtils.isEmpty(uid)) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		return loginService.deleteLoginByUid(uid);
	}

	/**
	 * 更新
	 * @param loginModel
	 * @return
	 */
	@RequestMapping(value = "updateLoginByUid", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<String> updateLoginByUid(@RequestBody LoginModel loginModel) {
		if (loginModel == null || StringUtils.isEmpty(loginModel.getUid())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		return loginService.updateLoginByUid(loginModel);
	}

	/**
	 * 根据登录方式查询login
	 * @param login		登录方式
	 * @return
	 */
	@RequestMapping(value = "queryLoginByLogin", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<LoginModel> queryLoginByLogin(String login) {
		if(StringUtils.isEmpty(login)) {
			return new ResultModel<LoginModel>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		LoginModel loginModel = loginService.queryLoginByLogin(login);
		if(loginModel == null) {
			return new ResultModel<LoginModel>(ResultModel.NOT_FOUND_OBJECT, "查询失败，数据不存在", null);
		}
		return new ResultModel<LoginModel>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, loginModel);
	}

	/**
	 * 通过uid查询user的登录方式 (一般有多种)
	 * 
	 * @param uid
	 *            user id
	 * @return
	 */
	@RequestMapping(value = "queryLoginByUid", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<List<LoginModel>> queryLoginByUid(String uid) {
		if (StringUtils.isEmpty(uid)) {
			return new ResultModel<List<LoginModel>>(ResultModel.ERR_PARAM, "请求参数有误", null);
		} 
		List<LoginModel> loginModels = loginService.queryLoginByUid(uid);
		if(Utils.isEmptyCollection(loginModels)) {
			return new ResultModel<List<LoginModel>>(ResultModel.NOT_FOUND_OBJECT, "查询失败，数据不存在", null);
		}
		return new ResultModel<List<LoginModel>>(ResultModel.SUCCESS, "成功获取login", loginModels);
	}
	
	/**
	 * 检查 login 登录方式
	 * 
	 * @param login
	 *            登录方式
	 * @param response
	 */
	@RequestMapping(value = "checkLogin")
	public void checkLogin(String login,HttpServletResponse response) {
		boolean isValidLogin = false;
		LoginModel loginModel;
		PrintWriter out = null;
		try {
			loginModel = loginService.queryLoginByLogin(login);
			if (loginModel == null) {
				isValidLogin = true;
			}
			out = response.getWriter();
			out.write(String.valueOf(isValidLogin));
		} catch (IOException e) {
			logger.warn("检查登录方式出错:)");
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
	
	/**
	 * 判断输入的密码是否正确
	 * 
	 * @param uid
	 * @param password
	 * @param response
	 */
	@RequestMapping(value = "checkPassword")
	public void checkPassword(@RequestParam(value = "uid", required = false) String uid, 
			@RequestParam("password") String password,
			HttpServletRequest request,
			HttpServletResponse response) {
		uid = (uid == null) ? UserUtil.getUidByRequest(request) : uid;
		boolean result = loginService.checkPassword(uid, password);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.write(String.valueOf(result));
		} catch (IOException e) {
			logger.warn("检查password出错:)");
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
}